import { useSocket } from '/@/stores/chat/socket'
import { useChatTalk } from '/@/stores/chat/talk'
import WsSocket from './ws-socket'
import { Session } from '/@/utils/storage';
import TalkEvent from '/@/views/chat/im-server/event/talk.js'
import LoginEvent from '/@/views/chat/im-server/event/login.js'
import RevokeEvent from '/@/views/chat/im-server/event/revoke.js'
import FriendApplyEvent from '/@/views/chat/im-server/event/friend-apply.js'
import GroupJoinEvent from '/@/views/chat/im-server/event/group-join.js'

/**
 * SocketInstance 连接实例
 *
 * 注释: 所有 WebSocket 消息接收处理在此实例中处理
 */
class SocketInstance {
    /**
     * WsSocket 实例
     */
    socket

    /**
     * SocketInstance 初始化实例
     */
    constructor() {
        const socketStore = useSocket()
        const chatTalk = useChatTalk()
        this.socket = new WsSocket(
            () => {
                let token = Session.get('token')

                if(token == ""){
                    window.location.reload()
                }
                return `${import.meta.env.VITE_WS_API_URL}?token=` + Session.get('token')
            },
            {
                // eslint-disable-next-line no-unused-vars
                onError: evt => {
                    console.log('Websocket 连接失败回调方法')
                },
                // Websocket 连接成功回调方法
                // eslint-disable-next-line no-unused-vars
                onOpen: evt => {
                    // 更新 WebSocket 连接状态
                    socketStore.UPDATE_SOCKET_STATUS(true)
                    chatTalk.LOAD_TALK_ITEMS()
                },
                // Websocket 断开连接回调方法
                // eslint-disable-next-line no-unused-vars
                onClose: evt => {
                    // 更新 WebSocket 连接状态
                    socketStore.UPDATE_SOCKET_STATUS(false)
                },
            }
        )

        this.registerEvents()
    }
    // 连接 WebSocket 服务
    connect() {
        this.socket.connection()
    }
    /**
     * 注册回调消息处理事件
     */
    registerEvents() {
        // eslint-disable-next-line no-unused-vars
        this.socket.on('heartbeat', data => {})

        this.socket.on('event_talk', data => {
            new TalkEvent(data).handle()
        })

        this.socket.on('event_login', data => {
            new LoginEvent(data).handle()
        })

        this.socket.on('event_talk_keyboard', data => {
            new KeyboardEvent(data).handle()
        })

        this.socket.on('event_talk_revoke', data => {
            new RevokeEvent(data).handle()
        })

        this.socket.on('event_contact_apply', data => {
            new FriendApplyEvent(data).handle()
        })

        this.socket.on('event_talk_join_group', data => {
            new GroupJoinEvent(data).handle()
        })

        this.socket.on('event_error', data => {
            Notification({
                title: '友情提示',
                message: data.message,
                type: 'warning',
            })
        })
    }

    /**
     * 聊天发送数据
     *
     * @param {Object} mesage
     */
    send(mesage) {
        this.socket.send(mesage)
    }

    /**
     * 推送消息
     *
     * @param {String} event 事件名
     * @param {Object} data 数据
     */
    emit(event, data) {
        this.socket.emit(event, data)
    }
}

export default new SocketInstance()
